package org.jeecg.modules.demo.workflow.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.jeecg.modules.demo.bean.entity.ActHiVarinst;
import org.jeecg.modules.demo.mapper.ActiHiVarinstMapper;
import org.jeecg.modules.demo.utils.R;
import org.jeecg.modules.demo.workflow.service.FormService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * @author : willian fu
 * @date : 2022/8/24
 */
@Tag(name = "流程表单")
@RestController
@RequestMapping("wflow/process/form")
public class ProcessFromController {

    @Autowired
    private FormService formService;

    @Autowired
    private ActiHiVarinstMapper actiHiVarinstMapper;

    /**
     * 获取流程实例表单数据
     * @param instanceId 流程实例ID
     * @return 该流程实例的表单数据
     */
    @GetMapping("data/by/{instanceId}")
    public Object getProcessInstanceFormData(@PathVariable String instanceId){
        return R.ok(formService.getProcessInstanceFormData(instanceId));
    }

    /**
     * 获取流程实例表单字段历史数据
     * @param instanceId 流程实例ID
     * @param fieldId 字段ID
     * @return 该流程实例的表单字段历史数据
     */
    @GetMapping("his/by/{instanceId}/{fieldId}")
    public Object getProcessInstanceFormDataHis(@PathVariable String instanceId,
                                                @PathVariable String fieldId){
        return R.ok(formService.getFormDataChangeLog(instanceId, fieldId));
    }

    /**
     *  填写内容更新到wflow中
     * @return 该流程实例的表单字段数据
     */
    @Operation(summary = "填写内容更新到Dailywflow中")
    @PostMapping("/editWflowDaily")
    public void editWflowDaily(@RequestBody JSONObject jsObject){
        String instanceId = jsObject.getString("instanceId");
        String text= jsObject.getString("text");
        String taskSource = jsObject.getString("taskSource");
        String description = jsObject.getString("description");
        String completionDeadline = jsObject.getString("completionDeadline");
        if (completionDeadline != null){

            actiHiVarinstMapper.update(null,new LambdaUpdateWrapper<ActHiVarinst>()
                    .eq(ActHiVarinst::getProcInstId, instanceId)
                    .eq(ActHiVarinst::getName, "field9174919887441")
                    .set(ActHiVarinst::getText,text));

            actiHiVarinstMapper.update(null,new LambdaUpdateWrapper<ActHiVarinst>()
                    .eq(ActHiVarinst::getProcInstId, instanceId)
                    .eq(ActHiVarinst::getName, "field3552319730324")
                    .set(ActHiVarinst::getText,taskSource));

            actiHiVarinstMapper.update(null,new LambdaUpdateWrapper<ActHiVarinst>()
                    .eq(ActHiVarinst::getProcInstId, instanceId)
                    .eq(ActHiVarinst::getName, "field2899705216964")
                    .set(ActHiVarinst::getText,description));

            actiHiVarinstMapper.update(null,new LambdaUpdateWrapper<ActHiVarinst>()
                    .eq(ActHiVarinst::getProcInstId, instanceId)
                    .eq(ActHiVarinst::getName, "field6872688718597")
                    .set(ActHiVarinst::getText,completionDeadline));


        }else
            actiHiVarinstMapper.update(null,new LambdaUpdateWrapper<ActHiVarinst>()
                    .eq(ActHiVarinst::getProcInstId, instanceId)
                    .eq(ActHiVarinst::getName, "field9174919887441")
                    .set(ActHiVarinst::getText,text));




    }

    /**
     *  填写内容更新到wflow中
     * @return 该流程实例的表单字段数据
     */
    @Operation(summary = "填写内容更新到ZyHazardwflow中")
    @PostMapping("/editWflowZyHazard")
    public void editWflowZyHazard(@RequestBody JSONObject jsObject) {
        // 1. 参数提取
        String instanceId = jsObject.getString("instanceId");
        String text = jsObject.getString("text");
        String commonProblem = jsObject.getString("commonProblem");
        String problemDescription = jsObject.getString("problemDescription");
        String measure = jsObject.getString("measure");
        String flag = jsObject.getString("flag");
        String rectificationDeadline = jsObject.getString("rectificationDeadline");
        String qualityType1 = jsObject.getString("qualityType");
        // 将 qualityType1 包装成 ["qualityType1"]
        String qualityType = "[" + qualityType1 + "]";

        // 2. 守卫模式：检查核心参数，如果为空则直接返回，防止误操作
        // 使用 StringUtils.isBlank 可以同时判断 null, 空字符串和纯空白字符
        if (StringUtils.isBlank(instanceId)) {
            // 建议记录一条日志，方便排查问题
            // log.warn("editWflowZyHazard called with empty instanceId. Request: {}", jsObject);
            return;
        }

        // 3. 使用 Map 存储所有需要更新的字段名和值，消除重复代码
        Map<String, String> fieldUpdates = new HashMap<>();
        fieldUpdates.put("field8203380757848", text);
        fieldUpdates.put("field6467880639149", commonProblem);
        fieldUpdates.put("field9728680661016", problemDescription);
        fieldUpdates.put("field6415480711298", measure);
        fieldUpdates.put("field6057178332583", flag);
        fieldUpdates.put("field5626912400097", qualityType);

        // 4. 处理条件性更新的字段
        if (StringUtils.isNotBlank(rectificationDeadline)) {
            fieldUpdates.put("field5746905932605", rectificationDeadline);
        }

        // 5. 遍历 Map，执行单次更新操作
        // 注意：这里仍然是循环调用 mapper，后面会介绍更优的批量更新方式
        for (Map.Entry<String, String> entry : fieldUpdates.entrySet()) {
            actiHiVarinstMapper.update(
                    null,
                    new LambdaUpdateWrapper<ActHiVarinst>()
                            .eq(ActHiVarinst::getProcInstId, instanceId)
                            .eq(ActHiVarinst::getName, entry.getKey())
                            .set(ActHiVarinst::getText, entry.getValue())
            );
        }
    }
//    public void editWflowZyHazard(@RequestBody JSONObject jsObject){
//        String instanceId = jsObject.getString("instanceId");
//        String text= jsObject.getString("text");
//        String commonProblem = jsObject.getString("commonProblem");
//        String problemDescription = jsObject.getString("problemDescription");
//        String measure = jsObject.getString("measure");
//        String flag = jsObject.getString("flag");
//        String rectificationDeadline = jsObject.getString("rectificationDeadline");
//        String qualityType1 = jsObject.getString("qualityType");
//        //将qualityType1包装成["qualityType1"]
//        String qualityType = "[" + qualityType1 + "]";
//        //如果rectificationDeadline，走下面的逻辑
//        if (rectificationDeadline != null) {
//            // 分开执行每个更新操作
//            //落实进展情况process
//            actiHiVarinstMapper.update(null, new LambdaUpdateWrapper<ActHiVarinst>()
//                    .eq(ActHiVarinst::getProcInstId, instanceId)
//                    .eq(ActHiVarinst::getName, "field8203380757848")
//                    .set(ActHiVarinst::getText, text));
//            //集团内部巡视通报共性问题commonProblem
//            actiHiVarinstMapper.update(null, new LambdaUpdateWrapper<ActHiVarinst>()
//                    .eq(ActHiVarinst::getProcInstId, instanceId)
//                    .eq(ActHiVarinst::getName, "field6467880639149")
//                    .set(ActHiVarinst::getText, commonProblem));
//            //自查问题problemDescription
//            actiHiVarinstMapper.update(null, new LambdaUpdateWrapper<ActHiVarinst>()
//                    .eq(ActHiVarinst::getProcInstId, instanceId)
//                    .eq(ActHiVarinst::getName, "field9728680661016")
//                    .set(ActHiVarinst::getText, problemDescription));
//            //措施measure
//            actiHiVarinstMapper.update(null, new LambdaUpdateWrapper<ActHiVarinst>()
//                    .eq(ActHiVarinst::getProcInstId, instanceId)
//                    .eq(ActHiVarinst::getName, "field6415480711298")
//                    .set(ActHiVarinst::getText, measure));
//            //标志flag
//            actiHiVarinstMapper.update(null, new LambdaUpdateWrapper<ActHiVarinst>()
//                    .eq(ActHiVarinst::getProcInstId, instanceId)
//                    .eq(ActHiVarinst::getName, "field6057178332583")
//                    .set(ActHiVarinst::getText, flag));
//            //整改期限rectificationDeadline
//            actiHiVarinstMapper.update(null, new LambdaUpdateWrapper<ActHiVarinst>()
//                    .eq(ActHiVarinst::getProcInstId, instanceId)
//                    .eq(ActHiVarinst::getName, "field5746905932605")
//                    .set(ActHiVarinst::getText, rectificationDeadline));
//            //质量类别qualityType
//            actiHiVarinstMapper.update(null, new LambdaUpdateWrapper<ActHiVarinst>()
//                    .eq(ActHiVarinst::getProcInstId, instanceId)
//                    .eq(ActHiVarinst::getName, "field5626912400097")
//                    .set(ActHiVarinst::getText, qualityType));
//        }else
//        // 分开执行每个更新操作
//        //落实进展情况process
//        actiHiVarinstMapper.update(null, new LambdaUpdateWrapper<ActHiVarinst>()
//                .eq(ActHiVarinst::getProcInstId, instanceId)
//                .eq(ActHiVarinst::getName, "field8203380757848")
//                .set(ActHiVarinst::getText, text));
//        //集团内部巡视通报共性问题commonProblem
//        actiHiVarinstMapper.update(null, new LambdaUpdateWrapper<ActHiVarinst>()
//                .eq(ActHiVarinst::getProcInstId, instanceId)
//                .eq(ActHiVarinst::getName, "field6467880639149")
//                .set(ActHiVarinst::getText, commonProblem));
//        //自查问题problemDescription
//        actiHiVarinstMapper.update(null, new LambdaUpdateWrapper<ActHiVarinst>()
//                .eq(ActHiVarinst::getProcInstId, instanceId)
//                .eq(ActHiVarinst::getName, "field9728680661016")
//                .set(ActHiVarinst::getText, problemDescription));
//        //措施measure
//        actiHiVarinstMapper.update(null, new LambdaUpdateWrapper<ActHiVarinst>()
//                .eq(ActHiVarinst::getProcInstId, instanceId)
//                .eq(ActHiVarinst::getName, "field6415480711298")
//                .set(ActHiVarinst::getText, measure));
//        //标志flag
//        actiHiVarinstMapper.update(null, new LambdaUpdateWrapper<ActHiVarinst>()
//                .eq(ActHiVarinst::getProcInstId, instanceId)
//                .eq(ActHiVarinst::getName, "field6057178332583")
//                .set(ActHiVarinst::getText, flag));
//    }




    /**
     *  填写内容更新到wflow中
     * @return 该流程实例的表单字段数据
     */
    @Operation(summary = "填写内容更新到ZyHazardwflow中")
    @PostMapping("/editWflowZyHazard1")
    public void editWflowZyHazard1(@RequestBody JSONObject jsObject){
        String instanceId = jsObject.getString("instanceId");
        String text= jsObject.getString("text");
        String commonProblem = jsObject.getString("commonProblem");
        String problemDescription = jsObject.getString("problemDescription");
        String measure = jsObject.getString("measure");
        String field = jsObject.getString("field");
        //逗号分割
        String[] fields = field.split(",");

        // 分开执行每个更新操作
        //落实进展情况process
        actiHiVarinstMapper.update(null, new LambdaUpdateWrapper<ActHiVarinst>()
                .eq(ActHiVarinst::getProcInstId, instanceId)
                .eq(ActHiVarinst::getName, fields[0])
                .set(ActHiVarinst::getText, text));
        //集团内部巡视通报共性问题commonProblem
        actiHiVarinstMapper.update(null, new LambdaUpdateWrapper<ActHiVarinst>()
                .eq(ActHiVarinst::getProcInstId, instanceId)
                .eq(ActHiVarinst::getName, fields[1])
                .set(ActHiVarinst::getText, commonProblem));
        //自查问题problemDescription
        actiHiVarinstMapper.update(null, new LambdaUpdateWrapper<ActHiVarinst>()
                .eq(ActHiVarinst::getProcInstId, instanceId)
                .eq(ActHiVarinst::getName, fields[2])
                .set(ActHiVarinst::getText, problemDescription));
        //措施measure
        actiHiVarinstMapper.update(null, new LambdaUpdateWrapper<ActHiVarinst>()
                .eq(ActHiVarinst::getProcInstId, instanceId)
                .eq(ActHiVarinst::getName, fields[3])
                .set(ActHiVarinst::getText, measure));
    }

}
